package com.erp.controller;

import com.erp.mapper.NoticeMapper;
import com.erp.mapper.UNoticeMapper;
import com.erp.mapper.UserMapper;
import com.erp.pojo.Notice;
import com.erp.pojo.UNotice;
import com.erp.pojo.User;
import com.erp.tool.PgTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@RestController
public class NoticeController {

    @Autowired
    private NoticeMapper mapper;

    @Autowired
    private UNoticeMapper uNoticeMapper;

    @Autowired
    private UserMapper userMapper;

    @RequestMapping("/insertNotice")
    public Map insertNotice(Notice notice, HttpServletRequest request){
        Map<String,Boolean>map=new HashMap<String,Boolean>();
        map.put("state",false);
        notice.setCreateDate(new Date());
        notice.setFlag(1);
        notice.setUId(Integer.parseInt(String.valueOf(request.getSession().getAttribute("id"))));
        int insert = mapper.insert(notice);
        if (insert>0){
            List<User> userID = userMapper.findUserID();
            List<UNotice>uNoticeList=new ArrayList<UNotice>();
            for (User user : userID) {
                UNotice uNotice=new UNotice();
                uNotice.setNId(notice.getNId());
                uNotice.setUId(user.getUId());
                uNotice.setFlag(1);
                uNotice.setState(1);
                uNoticeList.add(uNotice);
            }
            int i = uNoticeMapper.insertUNotice(uNoticeList);
            if (i>0){
                map.put("state",true);
            }
        }
        return  map;
    }


    @RequestMapping("/limitNoyice")
    public List limitUsers(int num,HttpServletRequest request){
        List<Object>allList = new ArrayList<Object>();
        int row=4;
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("id",request.getSession().getAttribute("id"));
        map.put("end",row);
        map.put("start", PgTool.getStart(num,row).get("start"));
        allList.add(PgTool.getPage(row, uNoticeMapper.countNotice(map)));
        allList.add(uNoticeMapper.limitByUserId(map));
        return allList;
    }

    @RequestMapping("/updateFlagByUserId")
    public Map updateFlagByUserId(HttpServletRequest request){
        Map<String,Boolean>map=new HashMap<String,Boolean>();
        map.put("state",false);
        int id=Integer.parseInt(String.valueOf(request.getSession().getAttribute("id")));
        int i = uNoticeMapper.updateFlagByUserId(id);
        if (i>0){
            map.put("state",true);
        }
        return  map;
    }
}
